<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>threadpool</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th bgcolor="#70b0f0" class="navbar-select"
          >&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://chrisarndt.de/projects/threadpool/">Threadpool</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        Module&nbsp;threadpool
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="threadpool-module.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<!-- ==================== MODULE DESCRIPTION ==================== -->
<h1 class="epydoc">Module threadpool</h1><p class="nomargin-top"><span class="codelink"><a href="threadpool-pysrc.html">source&nbsp;code</a></span></p>
<p>Easy to use object-oriented thread pool framework.</p>
<p>A thread pool is an object that maintains a pool of worker threads to perform
time consuming operations in parallel. It assigns jobs to the threads
by putting them in a work request queue, where they are picked up by the
next available thread. This then performs the requested operation in the
background and puts the results in another queue.</p>
<p>The thread pool object can then collect the results from all threads from
this queue as soon as they become available or after all threads have
finished their work. It's also possible, to define callbacks to handle
each result as it comes in.</p>
<p>The basic concept and some code was taken from the book &quot;Python in a Nutshell,
2nd edition&quot; by Alex Martelli, O'Reilly 2006, ISBN 0-596-10046-9, from section
14.5 &quot;Threaded Program Architecture&quot;. I wrapped the main program logic in the
ThreadPool class, added the WorkRequest class and the callback system and
tweaked the code here and there. Kudos also to Florent Aide for the exception
handling mechanism.</p>
<p>Basic usage:</p>
<pre class="rst-literal-block">
&gt;&gt;&gt; pool = ThreadPool(poolsize)
&gt;&gt;&gt; requests = makeRequests(some_callable, list_of_args, callback)
&gt;&gt;&gt; [pool.putRequest(req) for req in requests]
&gt;&gt;&gt; pool.wait()
</pre>
<p>See the end of the module code for a brief, annotated usage example.</p>
<p>Website : <a class="rst-reference external" href="http://chrisarndt.de/projects/threadpool/" target="_top">http://chrisarndt.de/projects/threadpool/</a></p>

<hr />
<div class="fields">      <p><strong>Version:</strong>
        1.2.5
      </p>
      <p><strong>Date:</strong>
        $Date: 2008-11-19 18:34:46 +0100 (Wed, 19 Nov 2008) $
      </p>
      <p><strong>Author:</strong>
        Christopher Arndt
      </p>
      <p><strong>License:</strong>
        MIT license
      </p>
</div><!-- ==================== CLASSES ==================== -->
<a name="section-Classes"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Classes</span></td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="threadpool.NoResultsPending-class.html" class="summary-name">NoResultsPending</a><br />
      All work requests have been processed.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="threadpool.NoWorkersAvailable-class.html" class="summary-name">NoWorkersAvailable</a><br />
      No worker threads available to process remaining requests.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="threadpool.WorkerThread-class.html" class="summary-name">WorkerThread</a><br />
      Background thread connected to the requests/results queues.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="threadpool.WorkRequest-class.html" class="summary-name">WorkRequest</a><br />
      A request to execute a callable for putting in the request queue later.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="threadpool.ThreadPool-class.html" class="summary-name">ThreadPool</a><br />
      A thread pool, distributing work requests and collecting results.
    </td>
  </tr>
</table>
<!-- ==================== FUNCTIONS ==================== -->
<a name="section-Functions"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Functions</span></td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="threadpool-module.html#makeRequests" class="summary-sig-name">makeRequests</a>(<span class="summary-sig-arg">callable_</span>,
        <span class="summary-sig-arg">args_list</span>,
        <span class="summary-sig-arg">callback</span>=<span class="summary-sig-default">None</span>,
        <span class="summary-sig-arg">exc_callback</span>=<span class="summary-sig-default">&lt;function _handle_thread_exception at 0x128f0b0&gt;</span>)</span><br />
      Create several work requests for same callable with different arguments.</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="threadpool-pysrc.html#makeRequests">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
</table>
<!-- ==================== FUNCTION DETAILS ==================== -->
<a name="section-FunctionDetails"></a>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Function Details</span></td>
</tr>
</table>
<a name="makeRequests"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">makeRequests</span>(<span class="sig-arg">callable_</span>,
        <span class="sig-arg">args_list</span>,
        <span class="sig-arg">callback</span>=<span class="sig-default">None</span>,
        <span class="sig-arg">exc_callback</span>=<span class="sig-default">&lt;function _handle_thread_exception at 0x128f0b0&gt;</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="threadpool-pysrc.html#makeRequests">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <p>Create several work requests for same callable with different arguments.</p>
<p>Convenience function for creating several work requests for the same
callable where each invocation of the callable receives different values
for its arguments.</p>
<p><tt class="rst-docutils literal"><span class="pre">args_list</span></tt> contains the parameters for each invocation of callable.
Each item in <tt class="rst-docutils literal"><span class="pre">args_list</span></tt> should be either a 2-item tuple of the list of
positional arguments and a dictionary of keyword arguments or a single,
non-tuple argument.</p>
<p>See docstring for <tt class="rst-docutils literal"><span class="pre">WorkRequest</span></tt> for info on <tt class="rst-docutils literal"><span class="pre">callback</span></tt> and
<tt class="rst-docutils literal"><span class="pre">exc_callback</span></tt>.</p>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th bgcolor="#70b0f0" class="navbar-select"
          >&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://chrisarndt.de/projects/threadpool/">Threadpool</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Wed Nov 19 18:43:47 2008
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
